//
//  BaseNavigationController.m
//  BaseLib
//
//  Created by CPX on 2018/5/21.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "BaseNavigationController.h"
#import "BaseViewController.h"

/**
 导航控制器基类
 @author yzl
 */
@interface BaseNavigationController ()<UIGestureRecognizerDelegate>

@property (nonatomic, assign) BOOL enableRightGesture;

@end

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (@available(iOS 9.0, *)) {
        self.navigationBar.semanticContentAttribute = [UIView appearance].semanticContentAttribute;
        self.view.semanticContentAttribute = [UIView appearance].semanticContentAttribute;
    }
    
    self.enableRightGesture = YES;
    self.interactivePopGestureRecognizer.delegate = self;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if (self.viewControllers.count == 1) {
        return NO;
    }
    if ([self.topViewController isKindOfClass:[BaseViewController class]]) {
        if ([self.topViewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
            BaseViewController *vc = (BaseViewController *)self.topViewController;
            self.enableRightGesture = [vc gestureRecognizerShouldBegin];
        }
    }
    
    return self.enableRightGesture;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([viewController isKindOfClass:[BaseViewController class]]) {
        if ([viewController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
            BaseViewController *vc = (BaseViewController *)viewController;
            self.enableRightGesture = [vc gestureRecognizerShouldBegin];
        }
    }
    
    [super pushViewController:viewController animated:animated];
}

- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
    self.enableRightGesture = YES;
    return [super popToRootViewControllerAnimated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    if (self.viewControllers.count == 1) {
        self.enableRightGesture = YES;
    } else {
        NSUInteger index = self.viewControllers.count - 2;
        UIViewController *destinationController = [self.viewControllers objectAtIndex:index];
        if ([destinationController isKindOfClass:[BaseViewController class]]) {
            if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
                BaseViewController *vc = (BaseViewController *)destinationController;
                self.enableRightGesture = [vc gestureRecognizerShouldBegin];
            }
        }
    }
    
    return [super popViewControllerAnimated:animated];
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count == 1) {
        self.enableRightGesture = YES;
    } else {
        UIViewController *destinationController = viewController;
        if ([destinationController isKindOfClass:[BaseViewController class]]) {
            if ([destinationController respondsToSelector:@selector(gestureRecognizerShouldBegin)]) {
                BaseViewController *vc = (BaseViewController *)destinationController;
                self.enableRightGesture = [vc gestureRecognizerShouldBegin];
            }
        }
    }
    
    return [super popToViewController:viewController animated:animated];
}

- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
